package window

import (
	"math"
)

func Avg(iterator Iterator) float64 {
	var result = 0.0
	var count = 0
	for iterator.Next() {
		bucket := iterator.Bucket()
		for _, v := range bucket.Points {
			result += v
		}
		count += bucket.Count
	}
	return result / float64(count)
}

func Sum(iterator Iterator) (c float64) {
	for iterator.Next() {
		bucket := iterator.Bucket()
		for _, v := range bucket.Points {
			c += v
		}
	}
	return
}

func Count(iterator Iterator) (c float64) {
	for iterator.Next() {
		bucket := iterator.Bucket()
		c += float64(bucket.Count)
	}
	return
}

func Min(iterator Iterator) (c float64) {
	flag := false
	for iterator.Next() {
		bucket := iterator.Bucket()
		for _, v := range bucket.Points {
			if !flag {
				c = v
				flag = true
			} else {
				c = math.Min(c, v)
			}
		}
	}
	return
}

func Max(iterator Iterator) (c float64) {
	flag := false
	for iterator.Next() {
		bucket := iterator.Bucket()
		for _, v := range bucket.Points {
			if !flag {
				c = v
				flag = true
			} else {
				c = math.Max(c, v)
			}
		}
	}
	return
}
